{extend name='table'}

{block name="button"}
{if isset($type) and $type eq 'index'}

<!--{if auth('sync')}-->
<a data-table-id="UserTable" class="layui-btn layui-btn-sm layui-btn-primary" data-queue="{:url('sync')}">{:lang('刷新数据')}</a>
<!--{/if}-->

<!--{if auth('state')}-->
<a data-confirm="确定要启用该用户吗？" data-table-id="UserTable" data-action="{:url('state')}" data-rule="id#{unid};status#0" class='layui-btn layui-btn-sm layui-btn-primary'>批量禁用</a>
<!--{/if}-->
{else}
<!--{if auth('state')}-->
<a data-confirm="确定要恢复这些账号吗？" data-table-id="UserTable" data-action="{:url('state')}" data-rule="id#{unid};status#1" class='layui-btn layui-btn-sm layui-btn-primary'>批量恢复</a>
<!--{/if}-->
{/if}
<!--{if auth('remove')}-->
<a data-confirm="确定永久删除这些账号吗？" data-table-id="UserTable" data-action='{:url("remove")}' data-rule="id#{unid}" class='layui-btn layui-btn-sm layui-btn-primary'>批量删除</a>
<!--{/if}-->
{/block}

{block name="content"}
<div class="layui-tab layui-tab-card">
    <ul class="layui-tab-title">
        {foreach ['index'=>'用户管理','recycle'=>'回 收 站'] as $k=>$v}
        {if isset($type) and $type eq $k}
        <li data-open="{:url('index')}?type={$k}" class="layui-this">{$v}</li>
        {else}
        <li data-open="{:url('index')}?type={$k}">{$v}</li>
        {/if}{/foreach}
    </ul>
    <div class="layui-tab-content">
        {include file='user/admin/index_search'}
        <table id="UserTable" data-line="3" data-url="{:request()->url()}" data-target-search="form.form-search"></table>
    </div>
</div>

<script>
    $(function () {
        $('#UserTable').layTable({
            even: true, height: 'full', sort: {field: 'sort desc,id', type: 'desc'}, cellExpandedMode: 'tips',
            cols: [[
                {field: 'id', hide: true}, {checkbox: true, fixed: true},
                // {field: 'sort', title: '排序权重', width: 100, align: 'center', sort: true, templet: '#SortInputTpl'},
                {
                    field: 'headimg', title: '头 像', width: 95, align: 'center', templet: function (d) {
                        if (!d.user || !d.user.headimg) return '';
                        return laytpl('<div>{{-showTableImage(d.user.headimg,true,"md")}}</div>').render(d)
                    }
                },
                {
                    title: '用户账号', minWidth: 180, templet: function (d) {
                        let tpls = [];
                        tpls.push('关联手机：<b class="color-blue">{{d.user.phone}}</b>')
                        tpls.push('用户编号：<b class="color-blue">{{d.user.code}}</b>')
                        tpls.push('用户等级：<b class="color-blue">VIP{{d.level_code}}</b> {{d.level_name||""}}')
                        return laytpl(tpls.join('<br>')).render(d);
                    }
                },
                {
                    title: '用户姓名', minWidth: 120, templet: function (d) {
                        let tpls = [];
                        tpls.push('用户姓名：{{d.user.username||"-"}}')
                        tpls.push('用户昵称：{{d.user.nickname||"-"}}')
                        tpls.push('注册时间：{{d.user.create_time||"-"}}')
                        return laytpl(tpls.join('<br>')).render(d);
                    }
                },
                {
                    title: '上1级头像', width: 95, align: 'center', templet: function (d) {
                        if (d.relation1 && d.relation1.user) {
                            return laytpl('<div>{{-showTableImage(d.relation1.user.headimg,true,"md")}}</div>').render(d);
                        } else if (d.relation0 && d.relation0.user) {
                            return laytpl('<div>{{-showTableImage(d.relation0.user.headimg,true,"md")}}</div>').render(d);
                        } else {
                            return '';
                        }
                    }
                },
                {
                    title: '上1级代理', minWidth: 200, templet: function (d) {
                        let tpls = [], left = '';
                        if (d.relation1 && d.relation1.user) {
                            left = '<span class="layui-badge layui-badge-middle layui-bg-green" style="line-height:14px;padding:3px 4px">永久绑定</span>';
                            tpls.push('手机：<b class="color-blue">{{d.relation1.user.phone}}</b>')
                            tpls.push('编号：<b class="color-blue">{{d.relation1.user.code}}</b>')
                            tpls.push('等级：<b class="color-blue">VIP{{d.relation1.level_code}}</b> {{d.relation1.level_name||""}}')
                        } else if (d.relation0 && d.relation0.user) {
                            left = '<span class="layui-badge layui-bg-red layui-badge-middle" style="line-height:14px;padding:3px 4px">临时绑定</span>';
                            tpls.push('手机：<b class="color-blue">{{d.relation0.user.phone}}</b>')
                            tpls.push('编号：<b class="color-blue">{{d.relation0.user.code}}</b>')
                            tpls.push('等级：<b class="color-blue">VIP{{d.relation0.level_code}}</b> {{d.relation0.level_name||""}}')
                        } else {
                            tpls.push('<div class="color-desc padding-top-20">未绑定上1级</div>')
                        }
                        return laytpl('<div class="flex">' + left + "<div>" + tpls.join('<br>') + "</div></div>").render(d);
                    }
                },
                {
                    field: 'headimg', title: '上2级头像', width: 95, align: 'center', templet: function (d) {
                        if (d.relation2 && d.relation2.user && d.relation2.user.headimg) {
                            return laytpl('<div>{{-showTableImage(d.relation2.user.headimg,true,"md")}}</div>').render(d);
                        } else {
                            return '';
                        }
                    }
                },
                {
                    title: '上2级代理', minWidth: 180, templet: function (d) {
                        let tpls = [];
                        if (d.relation2 && d.relation2.user) {
                            tpls.push('手机：<b class="color-blue">{{d.relation2.user.phone}}</b>')
                            tpls.push('编号：<b class="color-blue">{{d.relation2.user.code}}</b>')
                            tpls.push('等级：<b class="color-blue">VIP{{d.relation2.level_code}}</b> {{d.relation2.level_name||""}}')
                        } else {
                            tpls.push('<div class="color-desc padding-top-20">未绑定上2级</div>')
                        }
                        return laytpl(tpls.join('<br>')).render(d);
                    }
                },
                {field: 'status', title: '账号状态', align: 'center', minWidth: 110, width: '5%', templet: '#StatusSwitchTpl'},
                {toolbar: '#toolbar', title: '操作面板', align: 'center', minWidth: 160, width: '9%', fixed: 'right'}
            ]]
        });

        // 数据状态切换操作
        layui.form.on('switch(StatusSwitch)', function (obj) {
            let data = {id: obj.value, status: obj.elem.checked > 0 ? 1 : 0};
            $.form.load("{:url('state')}", data, 'post', function (ret) {
                if (ret.code < 1) $.msg.error(ret.info, 3, function () {
                    $('#UserTable').trigger('reload');
                }); else {
                    $('#UserTable').trigger('reload')
                }
                return false;
            }, false);
        });
    });
</script>

<!-- 数据状态切换模板 -->
<script type="text/html" id="StatusSwitchTpl">
    <div class="padding-top-20"></div>
    <!--{if auth("state")}-->
    <input type="checkbox" value="{{d.unid}}" lay-skin="switch" lay-text="已激活|已禁用" lay-filter="StatusSwitch" {{-d.user.status>0?'checked':''}}>
    <!--{else}-->
    {{-d.status ? '<b class="color-green">已启用</b>' : '<b class="color-red">已禁用</b>'}}
    <!--{/if}-->
</script>

<!-- 列表排序权重模板 -->
<script type="text/html" id="SortInputTpl">
    <div class="padding-top-20"></div>
    <input type="number" min="0" data-blur-number="0" data-action-blur="{:sysuri()}" data-value="id#{{d.id}};action#sort;sort#{value}" data-loading="false" value="{{d.sort}}" class="layui-input text-center">
</script>

<script type="text/html" id="toolbar">
    <div class="padding-top-10"></div>
    <!--{if auth('remove')}-->
    <a class="layui-btn layui-btn-sm layui-btn-danger" data-confirm="确定要永久删除此账号吗？" data-action="{:url('remove')}" data-value="id#{{d.unid}}">删 除</a>
    <!--{/if}-->
    <!--{if auth('parent')}-->
    <a class="layui-btn layui-btn-sm" data-modal="{:url('parent')}" data-value="id#{{d.id}}" data-title="设置上级代理">上 级</a>
    <!--{/if}-->
</script>
{/block}
